#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

// 判断素数
bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

// 计算各位数字平方和
int ssum(int n) {
    int sum = 0;
    while (n > 0) {
        int d = n % 10;
        sum += d * d;
        n /= 10;
    }
    return sum;
}
int isHappy(int n)
{
    int history[10001];
    int hlen = 0;
    if(n == 1) return 0;
    while(true)
    {
        n = ssum(n);
        if(n == 1)
            return hlen+1;
        else
        {           
            for(int i = 0; i<hlen; i++)
            {
                if(history[i] == n)
                    return -1;
            }
           history[hlen++] = n;
        }
    }
    return -1;
}


int happy[10001] = { };

int main()
{
    int A,B;
    cin >> A >> B;
    for(int i = 0; i<10001; i++)
    {
        happy[i] = -1;
    }
    
    for(int i = A; i<=B; i++)
    {
        happy[i] = isHappy(i);
        if(isPrime(i))
            happy[i] = happy[i] * 2;
    }
    //特立独行的幸福数，就是哪些没有被踩着肩膀奔向1的幸福数踩过的幸福数。所以下面检查[A,B]间的每一个幸福数，记下他们的足迹在ifTransition数组中，即哪些被踩过的幸福数。
    bool ifTransition[10001] = {false};
    for(int i = A; i<=B; i++)
    {
        if(happy[i]>=0)
        {
            int n = i;
            while(true)
            {
                n = ssum(n);
                ifTransition[n] = true;
                if(n == 1) 
                    break;
            }
        }
    }
    bool ifFind = false;
    for(int i = A; i<=B; i++)
    {
        if(happy[i] >= 0 && ifTransition[i] == false)
        {
           cout <<i<<" " << happy[i] << endl;
            ifFind = true;
        }
    }
    if(ifFind == false)
        cout << "SAD";
}